name: PublishedReleaseCI
# - Gets artifacts from S3
# - Sends it to JFROG Artifactory
# - Adds them to the release assets

on: # yamllint disable-line rule:truthy
  release:
    types:
    - published
  workflow_dispatch:
    inputs:
      tag:
        description: 'Release tag'
        required: true
        type: string

jobs:
  ReleasePublish:
    runs-on: [self-hosted, style-checker]
    steps:
    - name: Set tag from input
      if: github.event_name == 'workflow_dispatch'
      run: |
        echo "GITHUB_TAG=${{ github.event.inputs.tag }}" >> "$GITHUB_ENV"
    - name: Set tag from REF
      if: github.event_name == 'release'
      run: |
        echo "GITHUB_TAG=${GITHUB_REF#refs/tags/}" >> "$GITHUB_ENV"
    - name: Deploy packages and assets
      run: |
        curl --silent --data '' --no-buffer \
          '${{ secrets.PACKAGES_RELEASE_URL }}/release/'"${GITHUB_TAG}"'?binary=binary_darwin&binary=binary_darwin_aarch64&sync=true'
  ############################################################################################
  ##################################### Docker images  #######################################
  ############################################################################################
  DockerServerImages:
    runs-on: [self-hosted, style-checker]
    steps:
    - name: Set tag from input
      if: github.event_name == 'workflow_dispatch'
      run: |
        echo "GITHUB_TAG=${{ github.event.inputs.tag }}" >> "$GITHUB_ENV"
    - name: Set tag from REF
      if: github.event_name == 'release'
      run: |
        echo "GITHUB_TAG=${GITHUB_REF#refs/tags/}" >> "$GITHUB_ENV"
    - name: Check out repository code
      uses: ClickHouse/checkout@v1
      with:
        clear-repository: true
        fetch-depth: 0  # otherwise we will have no version info
        filter: tree:0
        ref: ${{ env.GITHUB_TAG }}
    - name: Check docker clickhouse/clickhouse-server building
      run: |
        cd "$GITHUB_WORKSPACE/tests/ci"
        export CHECK_NAME="Docker server image"
        python3 docker_server.py --release-type auto --version "$GITHUB_TAG" --check-name "$CHECK_NAME" --push
    - name: Check docker clickhouse/clickhouse-keeper building
      run: |
        cd "$GITHUB_WORKSPACE/tests/ci"
        export CHECK_NAME="Docker keeper image"
        python3 docker_server.py --release-type auto --version "$GITHUB_TAG" --check-name "$CHECK_NAME" --push
    - name: Cleanup
      if: always()
      run: |
        docker ps --quiet | xargs --no-run-if-empty docker kill ||:
        docker ps --all --quiet | xargs --no-run-if-empty docker rm -f ||:
        sudo rm -fr "$TEMP_PATH"
